# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=osgbullet
pkgbase=mingw-w64-${_realname}-git
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-git" "${MINGW_PACKAGE_PREFIX}-${_realname}-debug-git")
_ver_base=3.0.0
pkgver=3.0.0.265
pkgrel=2
pkgdesc="Bullet physics and OpenSceneGraph integration (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
license=('spdx:LGPL-2.1')
url="https://github.com/mccdo/osgbullet/"
depends=("${MINGW_PACKAGE_PREFIX}-bullet"
         "${MINGW_PACKAGE_PREFIX}-OpenSceneGraph"
         "${MINGW_PACKAGE_PREFIX}-osgworks")
makedepends=("git"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-OpenSceneGraph-debug"
             "${MINGW_PACKAGE_PREFIX}-osgworks-debug")
options=(!strip staticlibs !buildflags)
source=("${_realname}"::"git+https://github.com/mccdo/osgbullet.git"
        # https://github.com/mccdo/osgbullet/pull/37
        "0001-osg-3.5.6.patch")
sha256sums=('SKIP'
            '482b9893fbbd51fb5a4f3e0b253bdcac13952ef606e30bbbc71157dc156931a3')

pkgver() {
  cd "${srcdir}/${_realname}"
  printf "%s.%s" "${_ver_base}" "$(git rev-list --count HEAD)"
}

prepare() {
  cd "${srcdir}/${_realname}"

  patch -Np1 -i "${srcdir}/0001-osg-3.5.6.patch"
}

build() {
  msg2 "Building release"
  [[ -d "${srcdir}"/build-${MSYSTEM}-release ]] && rm -rf "${srcdir}"/build-${MSYSTEM}-release
  mkdir -p "${srcdir}"/build-${MSYSTEM}-release && cd "${srcdir}"/build-${MSYSTEM}-release

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  "${MINGW_PREFIX}"/bin/cmake.exe \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DCMAKE_BUILD_TYPE=Release \
    -DosgWorks_DIR="${MINGW_PREFIX}"/lib \
    ../${_realname}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .

  msg2 "Building debug"
  [[ -d "${srcdir}"/build-${MSYSTEM}-debug ]] && rm -rf "${srcdir}"/build-${MSYSTEM}-debug
  mkdir -p "${srcdir}"/build-${MSYSTEM}-debug && cd "${srcdir}"/build-${MSYSTEM}-debug

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DCMAKE_BUILD_TYPE=Debug \
    -DosgWorks_DIR="${MINGW_PREFIX}"/lib \
    ../${_realname}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package_osgbullet-git() {
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}" "${MINGW_PACKAGE_PREFIX}-${_realname}-svn")
  replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-svn)

  cd "${srcdir}"/build-${MSYSTEM}-release

  DESTDIR=${pkgdir} "${MINGW_PREFIX}"/bin/cmake.exe --install .

  # support relocation
  local MINGW_PREFIX_W=$(cygpath -m "${MINGW_PREFIX}")
  sed -i "s#${MINGW_PREFIX_W}#\${_IMPORT_PREFIX}#g" "${pkgdir}${MINGW_PREFIX}"/lib/osgbullet-targets-release.cmake
}

package_osgbullet-debug-git() {
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}-git=${pkgver}"
           "${MINGW_PACKAGE_PREFIX}-OpenSceneGraph-debug"
           "${MINGW_PACKAGE_PREFIX}-osgworks-debug")
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}-debug")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-debug" "${MINGW_PACKAGE_PREFIX}-${_realname}-degug-svn")
  replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-debug-svn)

  cd "${srcdir}"/build-${MSYSTEM}-debug

  DESTDIR=${pkgdir} "${MINGW_PREFIX}"/bin/cmake.exe --install .

  rm -rf ${pkgdir}${MINGW_PREFIX}/include
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/flagpoll
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/*.cmake
  rm -rf ${pkgdir}${MINGW_PREFIX}/share/osgBullet/data
  rm -rf ${pkgdir}${MINGW_PREFIX}/share/osgBullet/extra
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
